package com.tbit.main.util;

import com.tbit.main.exception.BaseException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 断言
 *
 * @author Leon
 * 2019年1月23日 下午3:54:55
 */
public abstract class Assert {

    /**
     * 非Null断言
     *
     * @param object
     * @param message
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new BaseException(message);
        }
    }

    /**
     * 非Null断言
     *
     * @param object
     * @param message
     */
    public static void notNull(Object object, String message, String lang) {
        if (object == null) {
            throw new BaseException(message, lang);
        }
    }

    /**
     * 空串断言
     *
     * @param text
     * @param message
     */
    public static void hasLength(String text, String message) {
        if (!(text != null && text.length() > 0)) {
            throw new BaseException(message);
        }
    }

    /**
     * 空串断言
     *
     * @param text
     * @param message
     */
    public static void hasLength(String text, String message, String lang) {
        if (!(text != null && text.length() > 0)) {
            throw new BaseException(message, lang);
        }
    }

    /**
     * 非空&小于指定长度
     *
     * @param text
     * @param message
     */
    public static void notNullCheckLength(String text,int length, String message) {
        if (text == null || text.isEmpty() || text.length() > length) {
            throw new BaseException(message);
        }
    }

    public static void main(String[] args) {
        UserDTO userDTO1 = new UserDTO(1, 1, 14);
        UserDTO userDTO2 = new UserDTO(1, 2, 24);
        UserDTO userDTO3 = new UserDTO(1, 3, 34);
        UserDTO userDTO4 = new UserDTO(2, 1, 44);

        List<UserDTO> userDTOList = new ArrayList<>();
        userDTOList.add(userDTO1);
        userDTOList.add(userDTO2);
        userDTOList.add(userDTO3);
        userDTOList.add(userDTO4);

        // 将userDTO转为sumDTO
        List<UserSumDTO> sumDTOList = userDTOList.stream().map(e -> {
            UserSumDTO userSumDTO = new UserSumDTO();
            userSumDTO.setId(e.getId());
            if (e.getType() == 1) {
                userSumDTO.setSum1(e.getSum());
            }
            if (2 == e.getType()) {
                userSumDTO.setSum2(e.getSum());
            }
            if (3 == e.getType()) {
                userSumDTO.setSum3(e.getSum());
            }
            return userSumDTO;
        }).collect(Collectors.toList());

        // 将sumDTO根据id合并
        List<UserSumDTO> collect = sumDTOList.stream().collect(Collectors.toMap(UserSumDTO::getId, a -> a, (o1, o2) -> {
            o1.setSum1(null == o1.getSum1() ? o2.getSum1() : o1.getSum1());
            o1.setSum2(null == o1.getSum2() ? o2.getSum2() : o1.getSum2());
            o1.setSum3(null == o1.getSum3() ? o2.getSum3() : o1.getSum3());
            return o1;
        })).values().stream().collect(Collectors.toList());
        System.out.println(Arrays.toString(collect.toArray()));
    }
}
